<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 登陆，注册，注销
 *
 *
 */
class Register extends CI_Controller
{
	function __construct()
    {
        parent::__construct();
    }
   
    
    // --------------------------------------------------------------------

    /**
	 * 登陆，注册 界面
	 *
	 *
	 */	
    function index()
    { 
		
	    $data = array();
		$data['css'][0] = "<link type='text/css' rel='stylesheet' href='".base_url()."css/login.css'>";
		$data['css'][1] = "<link type='text/css' rel='stylesheet' href='".base_url()."css/jq_login.css'>";
	    $data['title'] = '用户注册';

		$data['error_msg'] = $this->uri->segment(3, 0);
	    $this->load->view('login',$data);
    }
    
    // --------------------------------------------------------------------

    /**
	 * 验证名字是否已存在 ajax
	 *
	 *
	 */		
	function check_name()
	{
		$qs = query_string_to_array();
	    $name = $qs['tbxRegisterNickname'];
		$this->load->model('customer_model');
		$msg = array('Result' => true);
		if ($name){
		    if ($this->customer_model->check_name($name)){
				$msg = array('Result' => false);
			}				
		}
        
		echo json_encode($msg);
	}

    // --------------------------------------------------------------------

    /**
	 * 注册生成验证码
	 *
	 *
	 */	
	function yzm_img()
	{
		$this->load->helper('captcha');
		code();
	}
    
	// --------------------------------------------------------------------

    /**
	 * 检查注册验证码是否准确
	 *
	 *
	 */	
	function _check_yzm()
	{
		session_start();
        $Verifier = $this->input->post('tbxRegisterVerifier');
        if ($_SESSION['verify'] == $Verifier){
			//
		}else{
			redirect('register/index/verifier');
		}
		return;
	}	
    
	// --------------------------------------------------------------------

    /**
	 * 保存注册信息
	 *
	 *
	 */	
	function save()
	{
        $this->_check_yzm();

		$name = $this->input->post('tbxRegisterNickname');
		$password = $this->input->post('tbxRegisterPassword');
		$firstName = $this->input->post('tbxRegisterFirstName');
		$lastName = $this->input->post('tbxRegisterLastName');
		$birthDay = $this->input->post('tbxRegisterBirthday');
		$sex = $this->input->post('selRegisterSex');
		$city = $this->input->post('selRegisterCity');
        
        $this->load->library('form_validation');
        $this->set_save_form_rules();
        
		if (TRUE == $this->form_validation->run()){
			$this->load->model('customer_model');            
            $this->customer_model->name = $name;
		    $this->customer_model->password = $password;
		    $this->customer_model->firstName = $firstName;
		    $this->customer_model->lastName = $lastName;
		    $this->customer_model->birthDay = $birthDay;
		    $this->customer_model->sex = $sex;
		    $this->customer_model->city = $city;
			$this->customer_model->create_custom_login_info();
            $customer_id = $this->db->insert_id();      
            $this->customer_model->create_custom_base_info($customer_id);
            
			$customer = array(
                   'user_name'  => $name,
				   'user_id'  => $customer_id,
                   'user_in' => TRUE
               );
            $this->session->set_userdata($customer);
			redirect('reg_success');
		}else{
            redirect('login');
		}
	}
    
	// --------------------------------------------------------------------

    /**
	 * 注册验证规则
	 *
	 *
	 */	
	function set_save_form_rules()
    {
        $rules['tbxRegisterNickname'] = 'required|min_length[4]|max_length[20]|alpha_dash';	
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';	
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		$rules['tbxRegisterPassword'] = 'required|min_length[6]|max_length[16]|alpha_dash';
		//TODO other validations
		$this->form_validation->set_rules($rules);		
    }
}
